package com.knife.blog.logic.impl;

import java.util.Collection;
import java.util.List;

import com.knife.blog.domain.BlogInfo;
import com.knife.blog.domain.BlogMessage;
import com.knife.blog.domain.BlogUser;
import com.knife.web.tools.DbPageList;
import com.knife.web.tools.IPageList;

public class BlogMessageServiceImpl extends DAOSupportService implements
        com.knife.blog.logic.BlogMessageService {

    private static BlogMessageServiceImpl singleton = new BlogMessageServiceImpl();

    public static BlogMessageServiceImpl getInstance() {
        return singleton;
    }

    public boolean add(BlogMessage message) {
        boolean ret = false;
        message.setInputtime(new java.util.Date());
        //message.setStatus(0);
        ret = this.dao.save(message);
        return ret;
    }

    public int batchDelBlogMessage(String[] cids) {
        int ret = 0;
        for (int i = 0; i < cids.length; i++) {
            if (delBlogMessage(cids[i])) {
                ret++;
            }
        }
        return ret;
    }

    public int countUnCheckMessage(String cid) {
        int ret = 0;
        
        String tmpSql = "select count(*) from blogmessage where belonguser=? and auditing=0";
        java.util.Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(cid);
        Number num = (Number) this.dao.uniqueResult(tmpSql, paras);
        ret = Integer.parseInt(num.toString());
        return ret;
    }

    public boolean delBlogMessage(String cid) {
        boolean ret = false;
        ret = this.dao.del(getBlogMessage(cid));
        return ret;
    }

    public BlogMessage getBlogMessage(String cid) {
        return (BlogMessage) this.dao.get(BlogMessage.class, cid);
    }

    public IPageList queryBlogMessage(String belonguserid,
            int currentPage, int pageSize) {
        //String scope = "belonguser=? and (parentId is null or parentId='') order by inputtime ";
        String scope = "belonguser=? and auditing>0 order by inputtime desc";
        Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(belonguserid);
        return queryBlogMessage(scope, paras, currentPage, pageSize);
    }

    public IPageList queryBlogUnMessage(String belonguserid,
            int currentPage, int pageSize) {
        //String scope = "belonguser=? and (parentId is null or parentId='') order by inputtime ";
        String scope = "belonguser=? and auditing=0 order by inputtime desc";
        Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(belonguserid);
        return queryBlogMessage(scope, paras, currentPage, pageSize);
    }

    public IPageList queryBlogMessageByUsername(String username,
            int currentPage, int pageSize) {
        //String scope = "belonguser=? and (parentId is null or parentId='') order by inputtime ";
        BlogUser blogUser = BlogUserServiceImpl.getInstance().getBlogUser(username);
        String scope = "belonguser=? and auditing>0 order by inputtime desc";
        Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(blogUser.getCid());
        return queryBlogMessage(scope, paras, currentPage, pageSize);
    }

    public IPageList queryCircleMessage(String belongCid, int currentPage, int pageSize) {
        String scope = "belongcid=? and auditing>0 order by inputtime desc";
        Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(belongCid);
        return queryBlogMessage(scope, paras, currentPage, pageSize);

    }

    public IPageList queryAuditedMessage(String usernameid,
            int currentPage, int pageSize) {
        String scope = "belonguser=? and auditing=? order by inputtime desc";
        Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(usernameid);
        paras.add(new Integer(1));
        return queryBlogMessage(scope, paras, currentPage, pageSize);
    }

    public IPageList queryAuditedMessageByUsername(String username,
            int currentPage, int pageSize) {
        String scope = "belonguser=? and auditing=? order by inputtime desc";
        BlogInfo bloginfo = BlogUserServiceImpl.getInstance().getUserBlog("username", username);
        Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(bloginfo.getCid());
        paras.add(new Integer(1));
        return queryBlogMessage(scope, paras, currentPage, pageSize);

    }

    public IPageList queryBlogMessage(String scope, Collection paras,
            int currentPage, int pageSize) {
        DbPageList pList = new DbPageList(BlogMessage.class, scope, paras);
        pList.doList(currentPage, pageSize);
        return pList;
    }

    public boolean updateBlogMessage(BlogMessage message) {
        boolean ret = false;
        ret = this.dao.update(message);
        return ret;
    }

    public List getTopMessage(String username, int topNum) {
        BlogInfo bloginfo = BlogUserServiceImpl.getInstance().getUserBlog("username", username);
        String scope = "belonguser=? and auditing>0 order by inputtime desc";
        java.util.Collection<Object> paras = new java.util.ArrayList<Object>();
        paras.add(bloginfo.getCid());
        DbPageList pList = new DbPageList(BlogMessage.class, scope, paras);
        pList.doList(1, topNum);
        return pList.getResult();
    }
}
